package com.upload.spring.boot.autoconfigure;

import com.qiniu.common.Region;
import com.upload.spring.boot.chooser.UploadChooer;
import com.upload.spring.boot.config.ServerConfig;
import com.upload.spring.boot.properties.AliyunOSSProperties;
import com.upload.spring.boot.properties.QiNiuProperties;
import com.upload.spring.boot.properties.QiNiuTokenInMemory;
import com.upload.spring.boot.properties.UploadProperties;
import com.upload.spring.boot.server.AliyunUploadServer;
import com.upload.spring.boot.server.LocalUploadServer;
import com.upload.spring.boot.server.QiNiuUploadServer;
import com.upload.spring.boot.server.UploadCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lgc
 * @createDate 2020/7/28 - 17:08
 */
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties({UploadProperties.class, QiNiuProperties.class, AliyunOSSProperties.class})
public class UploadAutoConfiguration {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Bean
    @ConditionalOnMissingBean(UploadCustomizer.class)
    @ConditionalOnProperty(name = "upload.local.enable",havingValue = "true")
    public UploadCustomizer localUploadServer(UploadProperties uploadProperties){
        if(log.isDebugEnabled()){
            log.debug("启用本地上传服务");
        }
        LocalUploadServer localUploadServer = new LocalUploadServer();
        localUploadServer.setUploadProperties(uploadProperties);
        localUploadServer.setServerConfig(new ServerConfig());
        return localUploadServer;
    }

    /**
     * 七牛上传
     */
    @Bean
    public QiNiuTokenInMemory qiNiuTokenInMemory(QiNiuProperties config){
        QiNiuTokenInMemory qiNiuTokenInMemory = new QiNiuTokenInMemory();
        qiNiuTokenInMemory.setConfig(config);
        return qiNiuTokenInMemory;
    }

    @Bean
    @ConditionalOnProperty(name = "upload.qiniu.enable",havingValue = "true")
    public UploadCustomizer qiNiuUploadServer(QiNiuProperties config, QiNiuTokenInMemory qiNiuToken) {
        if(log.isDebugEnabled()){
            log.debug("启用七牛云上传服务");
        }
        return new QiNiuUploadServer(config,qiNiuToken);
    }

    /**
     * 阿里云OSS上传
     */
    @Bean
    @ConditionalOnProperty(name = "upload.aliyun.enable",havingValue = "true")
    public UploadCustomizer aliyunUploadServer(AliyunOSSProperties properties) {
        if(log.isDebugEnabled()){
            log.debug("启用阿里云上传服务");
        }
        return new AliyunUploadServer(properties);
    }


    @Bean
    @ConditionalOnBean(UploadCustomizer.class)
    public UploadChooer uploadChooer(){
        return new UploadChooer();
    }


}
